package com.example.server.db.tables

import com.example.commons.models.AppVersion
import com.example.commons.models.Article
import com.example.commons.models.User
//import kotlinx.datetime.LocalDateTime
//import kotlinx.datetime.toJavaLocalDateTime
//import kotlinx.datetime.toKotlinLocalDateTime
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.javatime.datetime

//import org.jetbrains.exposed.sql.kotlin.datetime.datetime

object Users : Table() {
    val id = long("id").autoIncrement()
    val username = varchar("username", 128)
    val nickname = varchar("nickname", 128)
    val password = varchar("password", 128)
    val createdAt = datetime("createdAt").clientDefault {
        java.time.LocalDateTime.now()//.toKotlinLocalDateTime()
    }.nullable()

    override val primaryKey = PrimaryKey(id)

    fun resultRowToUser(row: ResultRow): User =
        User(
            id = row[id],
            username = row[username],
            nickname = row[nickname],
            password = row[password],
            createdAt = row[createdAt]//?.toJavaLocalDateTime() //.dateTimeValue
        )
}

object Articles : Table() {
    val id = long("id").autoIncrement()
    val title = varchar("title", 128)
    val body = varchar("body", 1024)
    val userId = long("userId").references(Users.id).nullable()
    val createdAt = datetime("createdAt").clientDefault {
        java.time.LocalDateTime.now()//.toKotlinLocalDateTime()
    }.nullable()

    override val primaryKey = PrimaryKey(id)

    fun resultRowToArticle(row: ResultRow): Article =
        Article(
            id = row[id],
            userId = row[userId],
            title = row[title],
            body = row[body],
            createdAt = row[createdAt]//?.toJavaLocalDateTime()
        )
}

object AppVersions : Table() {
    val id = long("id").autoIncrement()
    val versionCode = integer("versionCode")
    val versionName = varchar("versionName", 128)
    val applicationId = varchar("applicationId", 128)
        .index()
        .default("demo")
    val variantName = varchar("variantName", 128).default("")
    val outputFile = varchar("outputFile", 256).default("")

    val platformType = varchar("platformType", 128).default("android")

    /**
     * 版本的描述信息
     */
    val description = text("description")

    val createdAt = datetime("createdAt").clientDefault {
        java.time.LocalDateTime.now()//.toKotlinLocalDateTime()
    }.nullable()

    override val primaryKey = PrimaryKey(id)

    fun resultRowToAppVersion(row: ResultRow): AppVersion =
        AppVersion(
            id = row[id],
            applicationId = row[applicationId],
            variantName = row[variantName],
            versionCode = row[versionCode],
            versionName = row[versionName],
            outputFile = row[outputFile],
            description = row[description],
//            path = row[AppVersions.path],
            platformType = row[platformType],
            createdAt = row.getOrNull(createdAt)//?.toJavaLocalDateTime() //getOrNull(createdAt)?.dateTimeValue,
        )
}

